package models

import (
	"os"

	"github.com/go-resty/resty/v2"
)

type DuPan struct {
	Host      string
	Path      string
	Key       string
	AppId     string
	AppKey    string
	SecretKey string
	SignKey   string
}

// 失败
func (c *DuPan) GetAuthQrcode() (res *resty.Response, sid string, err error) {
	// 发起授权请求
	resty := resty.New()
	// redirect_uri := url.QueryEscape("http://127.0.0.1:9360")
	uri := c.Host + c.Path + "?force_login=1&display=page&response_type=code&qrcode=1&redirect_uri=oob" + "&device_id=" + c.AppId + "&scope=basic,netdisk&client_id=" + c.AppKey
	response, err := resty.R().Get(uri)
	return response, "", err
}

// 失败
func (c *DuPan) GetAuthToken() (res *resty.Response, sid string, err error) {
	// 发起授权请求
	resty := resty.New()
	// redirect_uri := url.QueryEscape("http://127.0.0.1:9360")
	uri := c.Host + c.Path + "?grant_type=authorization_code&code=1188f1efa797d565e9d19dc5c55b2e58&redirect_uri=oob" + "&client_secret=" + c.SecretKey + "&client_id=" + c.AppKey
	response, err := resty.R().Get(uri)
	return response, "", err
}

func (c *DuPan) GetFileStreaming(path string, adToken string) (res *resty.Response, err error) {
	workDir, _ := os.Getwd()
	accessToken, err := readTokenFromFile(workDir + "/DuPanConfig")
	if err != nil {
		return nil, err
	}
	uri := c.Host + c.Path + "&path=" + path + "&type=M3U8_AUTO_480" + "&access_token=" + accessToken
	if adToken != "" {
		uri += "&adToken=" + adToken
	}
	resty := resty.New()
	response, err := resty.R().SetHeader("User-Agent", "xpanvideo;netdisk;iPhone13;ios-iphone;15.1;ts").Get(uri)
	return response, err
}
